﻿#include "CustomProgressCtrl.h"

CCustomProgressCtrl::CCustomProgressCtrl()
{
    m_clrBar = RGB(0, 0, 255); // Default Bar color
    m_clrBk = RGB(255, 255, 255); // Default Background color
}

void CCustomProgressCtrl::SetBarColor(COLORREF clr)
{
    m_clrBar = clr;
}

void CCustomProgressCtrl::SetBkColor(COLORREF clr)
{
    m_clrBk = clr;
}

BEGIN_MESSAGE_MAP(CCustomProgressCtrl, CProgressCtrl)
    ON_WM_PAINT()
END_MESSAGE_MAP()

void CCustomProgressCtrl::OnPaint()
{
    CPaintDC dc(this);
    CRect rect;
    GetClientRect(&rect);

    int nMax, nMin;
    GetRange(nMin, nMax);
    int nPos = GetPos();

    // Calculate the width of the progress bar
    int nWidth = (rect.Width() * (nPos - nMin)) / (nMax - nMin);

    // Draw the progress bar
    dc.FillSolidRect(0, 0, nWidth, rect.Height(), m_clrBar);

    // Draw the background
    dc.FillSolidRect(nWidth, 0, rect.Width() - nWidth, rect.Height(), m_clrBk);
}



BEGIN_MESSAGE_MAP(CDoubleBufferedStatic, CStatic)
    ON_WM_PAINT()
END_MESSAGE_MAP()

void CDoubleBufferedStatic::OnPaint()
{
    CPaintDC dc(this);
    CRect rect;
    GetClientRect(&rect);

    CDC memDC;
    memDC.CreateCompatibleDC(&dc);
    CBitmap bmp;
    bmp.CreateCompatibleBitmap(&dc, rect.Width(), rect.Height());
    CBitmap* pOldBmp = memDC.SelectObject(&bmp);

    // 填充背景
    memDC.FillSolidRect(rect, GetSysColor(COLOR_BTNFACE));

    // 绘制文本
    CString text;
    GetWindowText(text);
    memDC.SetBkMode(TRANSPARENT);
    memDC.DrawText(text, &rect, DT_LEFT);

    // 将双缓冲绘制的内容复制到目标DC
    dc.BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);

    memDC.SelectObject(pOldBmp);
    bmp.DeleteObject();
    memDC.DeleteDC();
}